package kylesfirstrobot;

import java.util.Vector;

import battlecode.common.Clock;
import battlecode.common.Direction;
import battlecode.common.GameConstants;
import battlecode.common.MapLocation;
import battlecode.common.RobotController;
import battlecode.common.RobotType;


public class RobotPlayer {
	public static void run(RobotController rc) {
		while (true) {
			try {
				if (rc.getType() == RobotType.SOLDIER) {
					if (rc.isActive() && rc.getTeamPower() > 0) {
						Direction dir = AimAtEnemyHQ(rc);
						if (Clock.getRoundNum() < 100) {
							dir = AimAtRV(rc);
						}
						if (rc.senseMine(rc.getLocation().add(dir))!=null) {
							rc.defuseMine(rc.getLocation().add(dir));
						}
						rc.move(dir);
					} else {
						rc.yield();
					}
				} else if (rc.getType() == RobotType.HQ) {
					if (rc.isActive()) {
						// Spawn a soldier
						Direction dir = rc.getLocation().directionTo(rc.senseEnemyHQLocation());
						if (rc.canMove(dir)) {
							rc.spawn(dir);
						}
					}
				}

				// End turn
				rc.yield();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static Direction NextDir(Direction dir, boolean isClockwise) {
		if (isClockwise) {
			if (dir == Direction.NORTH) {
				return Direction.NORTH_EAST;
			} else if (dir == Direction.NORTH_WEST) {
				return Direction.NORTH;
			} else if (dir == Direction.WEST) {
				return Direction.NORTH_WEST;
			} else if (dir == Direction.SOUTH_WEST) {
				return Direction.WEST;
			} else if (dir == Direction.SOUTH) {
				return Direction.SOUTH_WEST;
			} else if (dir == Direction.SOUTH_EAST) {
				return Direction.SOUTH;
			} else if (dir == Direction.EAST) {
				return Direction.SOUTH_EAST;
			} else if (dir == Direction.NORTH_EAST) {
				return Direction.EAST;
			}
		} else {
			if (dir == Direction.EAST) {
				return Direction.NORTH_EAST;
			} else if (dir == Direction.NORTH_EAST) {
				return Direction.NORTH;
			} else if (dir == Direction.NORTH) {
				return Direction.NORTH_WEST;
			} else if (dir == Direction.NORTH_WEST) {
				return Direction.WEST;
			} else if (dir == Direction.WEST) {
				return Direction.SOUTH_WEST;
			} else if (dir == Direction.SOUTH_WEST) {
				return Direction.SOUTH;
			} else if (dir == Direction.SOUTH) {
				return Direction.SOUTH_EAST;
			} else if (dir == Direction.SOUTH_EAST) {
				return Direction.EAST;
			}
		}
		
		return dir;
	}
	
	public static Direction AimAtEnemyHQ(RobotController rc) {
		Direction dir = rc.getLocation().directionTo(rc.senseEnemyHQLocation());
		
		while (!(rc.senseMine(rc.getLocation().add(dir))==null && rc.canMove(dir))) {
			dir = NextDir(dir, true);
		}
		
		return dir;
	}
	
	public static Direction AimAtRV(RobotController rc) {
		MapLocation rv = rc.senseHQLocation().add(rc.senseHQLocation().directionTo(rc.senseEnemyHQLocation())).add(rc.senseHQLocation().directionTo(rc.senseEnemyHQLocation())).add(rc.senseHQLocation().directionTo(rc.senseEnemyHQLocation()));
		Direction dir = rc.getLocation().directionTo((new MapLocation(12,10)));
		
		while (!(rc.senseMine(rc.getLocation().add(dir))==null && rc.canMove(dir))) {
			dir = NextDir(dir, true);
		}
		
		return dir;
	}
}
